﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;//使线程可以调用窗体控件
        }
        void ControlLight()
        {
            while (true)
            { //线程始终处于被启用状态
                Thread.Sleep(5000); //线程休眠5秒
                pictureBox1.Image = Image.FromFile("Yellow.png"); //黄灯
                Thread.Sleep(2000); //线程休眠2秒
                pictureBox1.Image = Image.FromFile("Red.png"); //红灯
                Thread.Sleep(8000); //线程休眠8秒
                pictureBox1.Image = Image.FromFile("Green.png"); //绿灯
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Thread th = new Thread(new ThreadStart(ControlLight));//创建线程对象
            th.Start();//启动线程
        }
    }
}
